import os
import shutil
from typing import Annotated, List

import typer

from apserver_mgr.shared import init_system, DEFAULT_SYSNO

app = typer.Typer()


@app.command(name="upload")
def apserver_upload(
    files: Annotated[List[str], typer.Argument(help="要上传的文件")],
    sysno: Annotated[str, typer.Option(help="系统代号")] = DEFAULT_SYSNO,
):
    """
    上传服务器文件。
    """
    console, sysinfo, servers = init_system(sysno)

    # 检查文件是否存在？
    for file in files:
        if not os.path.exists(file):
            typer.echo(f"文件 {file} 不存在")

    # 进度条总长度
    total = len(servers) * len(files)

    with typer.progressbar(length=total) as progress:
        for server in servers:
            for file in files:
                # 服务器上的路径是文件的基本名称，不带路径
                target_path = server.apdirectory + "\\" + os.path.basename(file)
                typer.echo(f"\t 上传 {target_path}")

                # 执行文件复制
                shutil.copy(file, target_path)

                # 更新进度条
                progress.update(1)
